package com.lhl.wx.cp.handler;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lhl.fw.core.event.CustomSpringEvent;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import org.springframework.context.ApplicationEventPublisher;

import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
public abstract class AbstractHandler implements WxCpMessageHandler {
    public static final String CONTACT_CHANGE = "contactChange";
    public static final String ENTER_AGENT = "enterAgent";
    public static final String LOCATION = "location";
    public static final String LOG = "log";
    public static final String MENU = "menu";
    public static final String MSG = "msg";
    public static final String NULL = "null";
    public static final String SCAN = "scan";
    public static final String SUBSCRIBE = "subscribe";
    public static final String UNSUBSCRIBE = "unsubscribe";

    public void send(WxCpXmlMessage wxMessage, Map<String, Object> context,String actionType) {
        final String msgType = wxMessage.getMsgType();
        if(MapUtil.isNotEmpty(context) && ObjectUtil.isNotEmpty(context.get("event"))){
            final ApplicationEventPublisher applicationEventPublisher = (ApplicationEventPublisher)context.get("event");
            //添加事件
            CustomSpringEvent<WxCpXmlMessage> customSpringEvent = new CustomSpringEvent<WxCpXmlMessage>(this, wxMessage,actionType);
            applicationEventPublisher.publishEvent(customSpringEvent);
        }
    }
}
